package com.pz.cloud.framework.leagueMem.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMemFee;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemFeeService;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.leagueMem.mapper.LeagueMemFeeMapper;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.util.BizUtil;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class LeagueMemFeeServiceImpl extends ServiceImpl<LeagueMemFeeMapper, LeagueMemFee> implements ILeagueMemFeeService {

    @Autowired
    private CommonDao commonDao;

    @Override
    public IPage<LeagueMemFee> findPage(LeagueMemFee entity, Map<String, String[]> parameterMap) {
        return commonDao.findPage(sqlBuilder(entity, parameterMap));
    }

    @Override
    public List<LeagueMemFee> list(LeagueMemFee leagueMemFee, Map<String, String[]> parameterMap) {
        return commonDao.findList(sqlBuilder(leagueMemFee, parameterMap));
    }

    private AbstractSqlBuilder sqlBuilder(LeagueMemFee entity, Map<String, String[]> parameterMap) {
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select lmf.*,u.username std_no,u.realname,s.college,s.major,s.grade,s.clazz from ")
                .appendTable(LeagueMemFee.class).append(" lmf ")
                .append(" inner join ").appendTable(LeagueMem.class).append(" lm on lm.id = lmf.mem_id and lm.del_flag = 0 ")
                .append(" inner join ").appendTable(SysUser.class).append(" u on u.id = lm.user_id and u.del_flag = 0 ")
                .append(" left join ").appendTable(Student.class).append(" s on lm.user_id = s.user_id and s.del_flag = 0 ")
                .returnType(LeagueMemFee.class)
                .page(entity)
                .joinCondition(entity, "lmf", parameterMap)
                .filterDataScope("lm", "user_id");

        if (entity.getStdNo() != null) {
            sqlBuilder.append(" and u.username like :stdNo").setParmsLike("stdNo", entity.getStdNo());
        }
        if (entity.getRealname() != null) {
            sqlBuilder.append(" and u.realname like :realname").setParmsLike("realname", entity.getRealname());
        }

        return sqlBuilder;
    }
}
